package webjoch.methods;

import java.awt.Point;
import java.util.ArrayList;

import org.rsbuddy.tabs.Inventory;

import com.rsbuddy.script.methods.Calculations;
import com.rsbuddy.script.methods.Menu;
import com.rsbuddy.script.methods.Mouse;
import com.rsbuddy.script.methods.Players;
import com.rsbuddy.script.task.Task;
import com.rsbuddy.script.wrappers.GroundItem;

import webjoch.misc.Constants;

/**
 * 
 * @author Webjoch
 *
 */
public class Items {
	/**
	 * Takes coins from the ground in the local room 
	 * @return returns true if there is money to take
	 */
	public static boolean getCoins(){
		GroundItem coins = Rooms.getLocalRoom().getGroundItem(Constants.COINS); 
		if (coins != null){
			interact(coins, "Take");
			Task.sleep(1000);
			return true;
		}
		return false;
	}
	
	/**
	 * Take all GroundItems that should be taken
	 * @return true if there are grounditems to take
	 */
	public static boolean getGroundItems(){
		ArrayList<GroundItem> allGroundItems = new ArrayList<GroundItem>();
		GroundItem coins = Rooms.getLocalRoom().getGroundItem(Constants.COINS);
		int free = 28 - Inventory.getCount();
		if (Rooms.getLocalRoom().getKey() != null){
			allGroundItems.add(Rooms.getLocalRoom().getKey());
			if (free == 0) Food.dropFood(1);
		}
		if (coins != null){
			allGroundItems.add(coins);
			if (free == 0 && !Inventory.contains(Constants.COINS)) Food.dropFood(1);
		}
		if (free - allGroundItems.size() > 0){
			for (GroundItem food : Food.getGroundFood(free - allGroundItems.size())){
				allGroundItems.add(food);
			}
		}
		GroundItem get = getNearestGroundItem(allGroundItems.toArray(new GroundItem[allGroundItems.size()]));
		int invCount = Inventory.getCount(true);
		if (get != null){
			if (interact(get, "Take")){
				for (int i = 0; i < 100 && invCount == Inventory.getCount(true); i++){
					if (Players.getLocal().isMoving()) i = 0;
					Task.sleep(20);
				}
			}
			return true;
		}
		return false;
	}
	
	/**
	 * Get nearest GroundItem from a GroundItems array
	 * @param groundItems GroundItems array
	 * @return the nearest GroundItem
	 */
	public static GroundItem getNearestGroundItem(GroundItem[] groundItems){
		GroundItem nearest = null;
		for (GroundItem g : groundItems){
			if (nearest == null){
				nearest = g;
				continue;
			}
			if (Calculations.distanceTo(g) < Calculations.distanceTo(nearest)){
				nearest = g;
			}
		}
		return nearest;
	}
	
	/**
	 * Interacting with a GroundItem
	 * @param i the GroundItem to interact with
	 * @param s the action to interact
	 * @return true if interacted
	 */
	public static boolean interact(GroundItem i, String s){
		if (Calculations.distanceTo(i) > 4 || !i.isOnScreen()){
			i.getLocation().clickOnMap();
			Task.sleep(1000);
		} else {
			if (Menu.isOpen()) Mouse.moveRandomly(100);
	        s = s.toLowerCase();
			Point   p1 = i.getLocation().getPoint(.5, .5, 0),
			        p2 = i.getLocation().getPoint(.5, .5, -400);
	        if (p1.x != 0 || p1.y != 0) {
	            Mouse.move(p1);
	            if (Menu.contains(s + " " + i.getItem().getName()))
	                return Menu.click(s + " " + i.getItem().getName());
	        }
	        if (p2.x != 0 || p2.y != 0) {
	            Mouse.move(p2);
	            if (Menu.contains(s + " " + i.getItem().getName())) {
	                return Menu.click(s + " " + i.getItem().getName());
	            }
	        }
		}
		return false;
	}
}
